import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_appgetwe/page/modelutils/video_list.dart';

class WorkItem {
  late int id;
  late String image;
  late int likes;

  WorkItem(this.id, this.image, this.likes);
}

class MyTableIndex extends StatefulWidget {
  @override
  _MyTableIndexState createState() => _MyTableIndexState();
}

class _MyTableIndexState extends State<MyTableIndex> {
  List<WorkItem> workItem = [];

  List<Widget> listWid = [];

  List<Widget> workItemtest = [];

  late ScrollController _controller;

  List<VideoList?> videoList = [];

  void _get() async {
    var result = await Dio().post(
        "https://188l877k45.zicp.vip/tkVideo/api/hch/tkVideo/find/all/1/10");

    List<VideoList?> mapList2 = await (result.data as List<dynamic>)
        .map((e) => VideoList.fromMap((e as Map<String, dynamic>)))
        .toList();

    if (mapList2.length > 0 && mapList2 != null) {
      setState(() {
        videoList = mapList2;
      });
    }

    print("数据的长度为" + mapList2.length.toString());
  }

  @override
  void initState() {
    //print(MediaQuery.of(context).size.width.toString());
    // TODO: implement initState
    super.initState();
    _controller = ScrollController();
    _controller.addListener(() {
      print(_controller.offset);
    });
    for (int i = 0; i < 20; i++) {
      workItem.add(new WorkItem(
          i, "http://rec6p45ah.hn-bkt.clouddn.com/575.jpg", i + 873));
    }

    _get();

    Future.delayed(Duration(seconds: 0), () {
      print('延时1s执行');
      if (videoList.length > 0) {
        print(videoList[1]!.imageUrl.toString());
      } else {
        print("空数据");
      }
      print("延时1s执行");



    });

    print(workItem[1].image);
    if (videoList.length > 0) {
      print(videoList[1]!.imageUrl.toString());
    } else {
      print("空数据");
    }

    workItemtest = [
      Container(
        child: Wrap(
          children: workItemtest.map((e) => Text("324")).toList(),
        ),
      )
    ];
    // try {
    //   listWid = [
    //     workItem.length > 0
    //         ? Wrap(
    //       direction: Axis.horizontal,
    //       spacing: 1,
    //       runSpacing: 1,
    //       children: workItem
    //           .map((e) => Column(
    //         children: [
    //           SizedBox(
    //             height: 1,
    //           ),
    //           GestureDetector(
    //             onTap: () {
    //               print(e.id.toString());
    //               //跳转详情路由传参
    //               if (e.id.toString() != null) {
    //                 Navigator.pushNamed(
    //                     context, "detailvideoindex",
    //                     arguments: e.id.toString());
    //               }
    //             },
    //             child: Container(
    //               width: 119,
    //               height: 150,
    //               //color: Colors.red,
    //               decoration: BoxDecoration(
    //                   image: DecorationImage(
    //                     image: NetworkImage(
    //                         "http://rec6p45ah.hn-bkt.clouddn.com/555.jpg"),
    //                     fit: BoxFit.fill,
    //                   )),
    //               child: Text(videoList.length.toString()),
    //             ),
    //           ),
    //         ],
    //       ))
    //           .toList(),
    //     )
    //         : Text("什么都没有.."),
    //     Wrap(
    //       direction: Axis.horizontal,
    //       spacing: 1,
    //       runSpacing: 1,
    //       children: workItem
    //           .map((e) => Column(
    //         children: [
    //           SizedBox(
    //             height: 1,
    //           ),
    //           Container(
    //             width: 119,
    //             height: 150,
    //             //color: Colors.red,
    //             decoration: BoxDecoration(
    //                 image: DecorationImage(
    //                   image: NetworkImage(e.image),
    //                   fit: BoxFit.fill,
    //                 )),
    //             //child: Image.network(""+e.image.toString()),
    //           ),
    //         ],
    //       ))
    //           .toList(),
    //     ),
    //     Wrap(
    //       direction: Axis.horizontal,
    //       spacing: 1,
    //       runSpacing: 1,
    //       children: workItem
    //           .map((e) => Column(
    //         children: [
    //           SizedBox(
    //             height: 1,
    //           ),
    //           Container(
    //             width: 119,
    //             height: 150,
    //             //color: Colors.red,
    //             decoration: BoxDecoration(
    //                 image: DecorationImage(
    //                   image: NetworkImage(e.image),
    //                   fit: BoxFit.fill,
    //                 )),
    //             //child: Image.network(""+e.image.toString()),
    //           ),
    //         ],
    //       ))
    //           .toList(),
    //     ),
    //     Wrap(
    //       direction: Axis.horizontal,
    //       spacing: 1,
    //       runSpacing: 1,
    //       children: workItem
    //           .map((e) => Column(
    //         children: [
    //           SizedBox(
    //             height: 1,
    //           ),
    //           Container(
    //             width: 119,
    //             height: 150,
    //             //color: Colors.red,
    //             decoration: BoxDecoration(
    //                 image: DecorationImage(
    //                   image: NetworkImage(e.image),
    //                   fit: BoxFit.fill,
    //                 )),
    //             //child: Image.network(""+e.image.toString()),
    //           ),
    //         ],
    //       ))
    //           .toList(),
    //     ),
    //     Wrap(
    //       direction: Axis.horizontal,
    //       spacing: 1,
    //       runSpacing: 1,
    //       children: workItem
    //           .map((e) => Column(
    //         children: [
    //           SizedBox(
    //             height: 1,
    //           ),
    //           Container(
    //             width: 119,
    //             height: 150,
    //             //color: Colors.red,
    //             decoration: BoxDecoration(
    //                 image: DecorationImage(
    //                   image: NetworkImage(e.image),
    //                   fit: BoxFit.fill,
    //                 )),
    //             //child: Image.network(""+e.image.toString()),
    //           ),
    //         ],
    //       ))
    //           .toList(),
    //     ),
    //     Wrap(
    //       direction: Axis.horizontal,
    //       spacing: 1,
    //       runSpacing: 1,
    //       children: workItem
    //           .map((e) => Column(
    //         children: [
    //           SizedBox(
    //             height: 1,
    //           ),
    //           Container(
    //             width: 119,
    //             height: 150,
    //             //color: Colors.red,
    //             decoration: BoxDecoration(
    //                 image: DecorationImage(
    //                   image: NetworkImage(e.image),
    //                   fit: BoxFit.fill,
    //                 )),
    //             //child: Image.network(""+e.image.toString()),
    //           ),
    //         ],
    //       ))
    //           .toList(),
    //     ),
    //   ];
    // } catch (e, s) {
    //   print("数据获取失败........");
    // }
  }

  List<Map> str = [
    {"id": 1, "name": "作品"},
    {"id": 2, "name": "私密"},
    {"id": 3, "name": "赞"},
    {"id": 4, "name": "收藏"},
    {"id": 5, "name": "评论"},
    {"id": 6, "name": "私信"},
  ];

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  bool isShow = true;

  int ids = 1;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          SizedBox(
            height: 5,
          ),
          Container(
            height: 26,
            child: ListView(
              controller: _controller,
              scrollDirection: Axis.horizontal,
              children: str
                  .map(
                    (e) => GestureDetector(
                        onTap: () {
                          print(e["id"].toString());
                          print(_controller.offset);
                          setState(() {
                            ids = e["id"];
                          });
                          switch (e["id"]) {
                            case 1:
                              _controller.animateTo(0,
                                  duration: Duration(seconds: 1),
                                  curve: Curves.ease);
                              break;
                            case 2:
                              _controller.animateTo(0,
                                  duration: Duration(seconds: 1),
                                  curve: Curves.ease);
                              break;
                            case 3:
                              _controller.animateTo(43,
                                  duration: Duration(seconds: 1),
                                  curve: Curves.ease);
                              break;
                            case 4:
                              _controller.animateTo(130,
                                  duration: Duration(seconds: 1),
                                  curve: Curves.ease);
                              break;
                            case 5:
                              _controller.animateTo(300,
                                  duration: Duration(seconds: 1),
                                  curve: Curves.ease);
                              break;
                          }
                        },
                        child: Container(
                          color: Colors.white,
                          alignment: Alignment.center,
                          width: 88,
                          //color: Colors.orange,
                          //margin: EdgeInsets.only(left: 10),
                          child: Column(
                            children: [
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  e["name"] == "作品"
                                      ? Text("")
                                      : e["name"] == "私密"
                                          ? Icon(
                                              Icons
                                                  .admin_panel_settings_outlined,
                                              color: ids == e["id"]
                                                  ? Color.fromRGBO(
                                                      34, 34, 34, 1)
                                                  : Color.fromRGBO(
                                                      116, 116, 116, 1),
                                              size: 18,
                                            )
                                          : e["name"] == "赞"
                                              ? Icon(
                                                  Icons
                                                      .admin_panel_settings_outlined,
                                                  color: ids == e["id"]
                                                      ? Color.fromRGBO(
                                                          34, 34, 34, 1)
                                                      : Color.fromRGBO(
                                                          116, 116, 116, 1),
                                                  size: 18,
                                                )
                                              : Text(""),
                                  Text(
                                    e['name'],
                                    style: TextStyle(
                                        fontSize: 12,
                                        color: ids == e["id"]
                                            ? Color.fromRGBO(34, 34, 34, 1)
                                            : Color.fromRGBO(116, 116, 116, 1)),
                                  ),
                                  Text(
                                    "" + e['name'] == "作品"
                                        ? 11.toString()
                                        : e['name'] == "私密"
                                            ? 12.toString()
                                            : e['name'] == "赞"
                                                ? 13.toString()
                                                : 14.toString(),
                                    style: TextStyle(
                                        color: ids == e["id"]
                                            ? Color.fromRGBO(34, 34, 34, 1)
                                            : Color.fromRGBO(116, 116, 116, 1)),
                                  ),
                                ],
                              ),
                              SizedBox(
                                height: 6,
                              ),
                              ids == e["id"]
                                  ? Container(
                                      alignment: Alignment.center,
                                      width: 20,
                                      height: 2,
                                      decoration: BoxDecoration(
                                        border: Border(
                                          bottom: BorderSide(
                                            color:
                                                Color.fromRGBO(21, 21, 21, 1),
                                            width: 2,
                                          ),
                                        ),
                                      ),
                                    )
                                  : Container(
                                      alignment: Alignment.center,
                                      width: 20,
                                      height: 2,
                                    ),
                            ],
                          ),
                        )),
                  )
                  .toList(),
            ),
          ),
          //Text(videoList.length.toString(),style: TextStyle(color: Colors.orange),),
          ids == 1
              ? Wrap(
            direction: Axis.horizontal,
            spacing: 1,
            runSpacing: 1,
            children: videoList
                .map((e) => Column(
              children: [
                SizedBox(
                  height: 1,
                ),
                Container(
                  width: 119,
                  height: 150,
                  //color: Colors.red,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                        image: NetworkImage(e!.imageUrl.toString()),
                        fit: BoxFit.fill,
                      )),
                  //child: Image.network(""+e.image.toString()),
                ),
              ],
            ))
                .toList(),
          )
              : ids == 2
                  ? Wrap(
            direction: Axis.horizontal,
            spacing: 1,
            runSpacing: 1,
            children: workItem
                .map((e) => Column(
              children: [
                SizedBox(
                  height: 1,
                ),
                Container(
                  width: 119,
                  height: 150,
                  //color: Colors.red,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                        image: NetworkImage(e.image),
                        fit: BoxFit.fill,
                      )),
                  //child: Image.network(""+e.image.toString()),
                ),
              ],
            ))
                .toList(),
          )
                  : ids == 3
                      ? Wrap(
            direction: Axis.horizontal,
            spacing: 1,
            runSpacing: 1,
            children: workItem
                .map((e) => Column(
              children: [
                SizedBox(
                  height: 1,
                ),
                Container(
                  width: 119,
                  height: 150,
                  //color: Colors.red,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                        image: NetworkImage(e.image),
                        fit: BoxFit.fill,
                      )),
                  //child: Image.network(""+e.image.toString()),
                ),
              ],
            ))
                .toList(),
          )
                      : ids == 4
                          ? Wrap(
            direction: Axis.horizontal,
            spacing: 1,
            runSpacing: 1,
            children: workItem
                .map((e) => Column(
              children: [
                SizedBox(
                  height: 1,
                ),
                Container(
                  width: 119,
                  height: 150,
                  //color: Colors.red,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                        image: NetworkImage(e.image),
                        fit: BoxFit.fill,
                      )),
                  //child: Image.network(""+e.image.toString()),
                ),
              ],
            ))
                .toList(),
          )
                          : ids == 5
                              ? Wrap(
            direction: Axis.horizontal,
            spacing: 1,
            runSpacing: 1,
            children: workItem
                .map((e) => Column(
              children: [
                SizedBox(
                  height: 1,
                ),
                Container(
                  width: 119,
                  height: 150,
                  //color: Colors.red,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                        image: NetworkImage(e.image),
                        fit: BoxFit.fill,
                      )),
                  //child: Image.network(""+e.image.toString()),
                ),
              ],
            ))
                .toList(),
          )
                              : Wrap(
            direction: Axis.horizontal,
            spacing: 1,
            runSpacing: 1,
            children: workItem
                .map((e) => Column(
              children: [
                SizedBox(
                  height: 1,
                ),
                Container(
                  width: 119,
                  height: 150,
                  //color: Colors.red,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                        image: NetworkImage(e.image),
                        fit: BoxFit.fill,
                      )),
                  //child: Image.network(""+e.image.toString()),
                ),
              ],
            ))
                .toList(),
          )
        ],
      ),
    );
  }
}
